#! /bin/sh
#接收外部参数
harbor_url="192.168.0.102:85"
harbor_project="gulimall"
#例如: gulimall-auth-server
project_name=$1
#例如: docker pull 192.168.0.102:85/gulimall/gulimall-auth-server:latest
tag="latest"
port1=$2
port0=$3

imageName=$harbor_url/$harbor_project/$project_name:$tag

echo "$imageName"

#查询容器是否存在，存在则删除
containerId=`docker ps -a | grep -w ${project_name}:${tag}  | awk '{print $1}'`
if [ "$containerId" !=  "" ] ; then
    echo "容器名=$containerId"
    #停掉容器
    docker stop $containerId

    #删除容器
    docker rm $containerId

	echo "成功删除容器"
fi

#查询镜像是否存在，存在则删除
imageId=`docker images | grep -w $project_name  | awk '{print $3}'`

if [ "$imageId" !=  "" ] ; then

    #删除镜像
    docker rmi -f $imageId

	echo "成功删除镜像"
fi

# 登录Harbor: docker login -u sandy -p Sandy123456 192.168.230.130:85
docker login -u admin -p Harbor12345 $harbor_url

# 下载镜像
docker pull $imageName

# 启动容器
docker run -di -p $port1:$port0 $imageName

echo "容器启动成功"
